import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { DataConfigService } from './data-config.service';
import {PermissionService} from '../../../service/permission.service';

@Component({
    selector: 'app-data-config',
    templateUrl: './data-config.component.html',
    styleUrls: ['./data-config.component.scss']
})
export class DataConfigComponent implements OnInit {

    /**
     * 数据源
     */
    listOfData?: any;
    /**
     * 表格加载
     */
    tableLoading: boolean = false;

    popupWayNum: any = null;
    queryForm: any = {
        popupWay: '-10',
        showFloatingWindowWay: "1"
    }
    /**
     * 加载失败显示图像占位符
     */
    fallback =
        '';
    constructor(
        private dataConfigService: DataConfigService,
        private message: NzMessageService,
        public permission: PermissionService,
    ) { }

    ngOnInit() {
        this.getConfigList();
    }

    getConfigList() {
        this.dataConfigService.getList().subscribe((res: any) => {
            this.queryForm.popupWay = res.data.pcI4Window > 0 ? 1 + "" : res.data.pcI4Window + "";
            this.popupWayNum = res.data.pcI4Window > 0 ? res.data.pcI4Window : null;
            this.queryForm.showFloatingWindowWay = res.data.showFloatingWindowWay + "";

            this.listOfData = res.data;
            this.tableLoading = false;
        }, err => {
            this.tableLoading = false;
        })
    }

    /**
     * 保存
     */
    save() {
        // let popupWay = this.queryForm.popupWay;
        let json = { ...this.queryForm };
        if (json.popupWay == 1) {
            json.popupWay = this.popupWayNum;
        }
        this.dataConfigService.save(json).subscribe((res: any) => {

            this.createMessage('success', '保存成功');
        })
    }

    /**
    * 全局展示操作反馈信息
    * @param type 其他提示类型 success:成功 error:失败 warning:警告
    */
    createMessage(type: any, str: any): void {
        this.message.create(type, str);
    }
}
